package resfoods.controller;

import com.yc.entity.AddrInfo;
import com.yc.entity.MemberInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.relational.core.sql.In;
import org.springframework.web.bind.annotation.*;
import resfoods.biz.AddrBiz;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("addr")
public class AddrController {
    @Autowired
    private AddrBiz addrBiz;

    @RequestMapping(value = "findBymno",method = RequestMethod.GET)
    public List<AddrInfo> findBymno(HttpSession session){
        MemberInfo memberInfo = (MemberInfo)session.getAttribute("user");
        List<AddrInfo> addrInfoList=null;
        if(memberInfo!=null){
            Integer mno = memberInfo.getMno();
            System.out.println(mno);
            addrInfoList = addrBiz.findBymno(mno);
        }
        return addrInfoList;
    }

    @RequestMapping(value = "add_address",method = RequestMethod.GET)
    public int addAddress(@RequestParam Integer mno,
                          @RequestParam String name, @RequestParam String tel,
                          @RequestParam String province, @RequestParam String city,
                          @RequestParam String area, @RequestParam String addr){
        AddrInfo addrInfo = new AddrInfo();
        String ano = null;
        try {
            ano = String.valueOf(Math.random()+System.nanoTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        addrInfo.setAno(ano);
        addrInfo.setMno(mno);
        addrInfo.setName(name);
        addrInfo.setTel(tel);
        addrInfo.setProvince(province);
        addrInfo.setCity(city);
        addrInfo.setArea(area);
        addrInfo.setAddr(addr);
        int result = addrBiz.add(addrInfo);
        return result;
    }
}
